<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <title>添加班级</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../static/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../static/layuimini/css/public.css" media="all">
    <style>
        .layui-form-item .layui-input-company {
            width: auto;
            padding-right: 10px;
            line-height: 38px;
        }
    </style>

    <script src="../../../static/layuimini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="../../../static/layuimini/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <script src1="http://local.res.layui.com/layui/release/laydate/dist/laydate.js" charset="utf-8"></script>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div class="layui-form layuimini-form" lay-filter="first">
            <div class="layui-form-item">
                <label class="layui-form-label required">系别</label>
                <div class="layui-input-block">
                    <select name="department" lay-verify="required" lay-verType="tips" lay-filter="department"
                            id="department">
                        <option value="">请选择系别</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">专业</label>
                <div class="layui-input-block">
                    <select name="major" lay-verify="required" lay-verType="tips" lay-filter="major" id="major">
                        <option value="">请选择专业</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">年级</label>
                <div class="layui-input-block">
                    <select name="grade" lay-verify="required" lay-verType="tips" lay-filter="grade" id="grade">
                        <option value="">请选择年级</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">班级名称</label>
                <div class="layui-input-block">
                    <input type="text" name="className" lay-verify="required" lay-reqtext="班级名称不能为空"
                           placeholder="请输入班级名称"
                           value="" class="layui-input" id="className">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['form', 'miniTab', 'jquery'], function () {
        var form = layui.form,
            layer = layui.layer,
            miniTab = layui.miniTab,
            $ = layui.jquery;

        //初始化惜别下拉框
        $.get("/getDepartment", function (data) {
            $.each(data, function (index, element) {
                $("#department").append("<option value='" + element.departmentName + "'>" + element.departmentName + "</option>");
            });
            layui.form.render('select');
        }, "json");

        //事件监听
        form.on('select(department)', function (data) {
            // console.log('select: ', this, data);
            console.log(data);
            let departmentName = $("#department").val();
            console.log("departmentName = " + departmentName);
            $.get("/getMajorByDepartment", {departmentName: departmentName}, function (data) {
                $("#major").empty();
                $("#major").append("<option value=''>请选择专业</option>");
                $("#grade").empty();
                $("#grade").append("<option value=''>请选择年级</option>");
                $.each(data, function (index, element) {
                    $("#major").append("<option value='" + element.majorName + "'>" + element.majorName + "</option>");
                });
                layui.form.render('select');
            }, "json");
        });
        //事件监听
        form.on('select(major)', function (data) {
            // console.log('select: ', this, data);
            console.log(data);
            let majorName = $("#major").val();
            $.get("/getGradeByMajor", {majorName: majorName}, function (data) {
                $("#grade").empty();
                $("#grade").append("<option value=''>请选择年级</option>");
                $.each(data, function (index, element) {
                    $("#grade").append("<option value='" + element.gradeName + "'>" + element.gradeName + "</option>");
                });
                layui.form.render('select');
            }, "json");
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let departmentName = $("#department").val();
            let majorName = $("#major").val();
            let gradeName = $("#grade").val();
            let className = $("#className").val();

            $.get("/addClass", {
                department: departmentName,
                major: majorName,
                grade: gradeName,
                className: className
            }, function (data) {
                if (data == "success") {
                    layer.msg('班级添加成功', {
                        icon: 1,
                        time: 1000, //1秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        // parent.layer.closeAll('iframe');
                        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        parent.window.location.reload();//刷新页面
                    });
                } else {
                    layer.msg('班级添加失败，请稍后再试！', {
                        icon: 0,
                        time: 1000, //1秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        // parent.layer.closeAll('iframe');
                        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    });
                }
            }, "text");
        });

        form.on('select(grade)', function (data) {
            console.log('select.grade：', this, data);
        });

    });
</script>
</body>
</html>